
//  不要在 onLaunch 的时候调用 getCurrentPage()此时 page 还没有生成。 
// getCurrentPage是获取当前页面的实例对象。

var util = require('../../../utils/util.js');
var app = getApp();


Page({

  data: {

    picture: "../../../image/0414couser.png",
    name: "",
    showdocinfor: false,
    hospitalArray: null,
    hospitalIDArray: null,
    index: 0,

    //科室下拉列表
    categoryArray: null,
    categoryIDArray: null,
    cindex: 0,

    levelArray: null,
    levelIDArray: null,
    lindex: 0,

    message: null,
    messagehid: true

  },

  switchChange: function (e) {

    this.setData({ showdocinfor: e.detail.value });

  },

  bindHospitalPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    });

    this.loadCategory();


  },

  bindCategoryPickerChange: function (e) {
    this.setData({
      cindex: e.detail.value
    });
  },

  bindLevelPickerChange: function (e) {
    this.setData({
      lindex: e.detail.value
    });
  },



  formSubmit: function (e) {
    var that = this;

    if (e.detail.value.name.length == 0) {
      util.showRejectToast('姓名不能为空');
      return;
    }

    if (e.detail.value.pid.length == 0) {
      util.showRejectToast('身份证不能为空');
      return;
    } else {

      var isIDCard2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
      var isIDCard1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;

      var result1 = isIDCard1.exec(e.detail.value.pid);
      var result2 = isIDCard2.exec(e.detail.value.pid);


      if (result1 == null && result2 == null) {
        util.showRejectToast('不是合法的身份证号码');
        return;
      }

    }

    var phone = /^1[0-9]{10}$/;
    var resultphone = phone.exec(e.detail.value.telephone);
    if (resultphone == null) {

      util.showRejectToast('不是合法的手机号码');
      return;

    }

    var emailr = /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/;
    var resultemail = emailr.exec(e.detail.value.email);
    if (resultemail == null) {
      util.showRejectToast('不是合法的电子邮件!');
      return;

    }



    if (this.data.showdocinfor) {

      if (this.data.index == 0) {
        util.showRejectToast('请选择医院');
        return;

      }

      if (this.data.cindex == 0) {
        util.showRejectToast('请选择科室');
        return;

      }
    }

    var hospitalid = null;
    if (that.data.hospitalIDArray != null) {
      hospitalid = that.data.hospitalIDArray[that.data.index];
    }

    var categoryid = null;
    if (that.data.categoryIDArray != null) {
      categoryid = that.data.categoryIDArray[that.data.cindex];
    }

    var levelid = null;
    if (that.data.levelIDArray != null) {
      levelid = that.data.levelIDArray[that.data.lindex];
    }


    var atkey = wx.getStorageSync("AccessToken");
    wx.request({

      url: app.globalData.addUserInfor,

      header: {

        "Content-Type": "application/x-www-form-urlencoded"

      },

      method: "POST",

      data: {
        AccessToken: atkey, USERNAME: e.detail.value.username, PIC: that.data.picture, PHONE: e.detail.value.telephone, PID: e.detail.value.pid, NAME: e.detail.value.name, EMAIL: e.detail.value.email,
        ISDOCTOR: that.data.showdocinfor, HOSPITAL_ID: hospitalid, CATEGORY_ID: categoryid, LEVEL_ID:levelid,DESCRIPTION: e.detail.value.description
      },

      success: function (res) {

        if (res.data.status == 0) {

          wx.showToast({

            title: res.data.info,

            icon: 'loading',

            duration: 1500

          })

        } else {

          if (res.data.result) {
       
            if (that.data.showdocinfor){
              wx.setStorageSync("UserType", "2");
            } else {
              wx.setStorageSync("UserType", "1");
            }

            util.showToastAndRedirect('/pages/me/userinfor/userinfor');


          } else {
            util.showRejectToast(res.data.reason);
          }

        }

      }

    })


  },

  loadCategory: function () {
    if (this.data.index == 0) {
      util.showRejectToast('请先选择医院');

      return;
    }

    var that = this;
    var atkey = wx.getStorageSync("AccessToken");
    var hid = this.data.hospitalIDArray[this.data.index];
    // --------- 发送凭证 ------------------
    wx.request({
      url: app.globalData.getCategoryList,
      data: {
        AccessToken: atkey,
        hospitalID: hid
      },
      header: {
        'content-type': 'application/json'
      },

      success: function (res) {

        if (!res.data.result) {
          util.showRejectToast(res.data.reason);
          return;
        }

        that.setData({
          categoryArray: res.data.datum.values,
          categoryIDArray: res.data.datum.ids
        });

      },
      fail: function () {
        util.showRejectToast('加载科室信息失败！');
      }

    })


  },

  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {

    var that = this;
    this.setData({
      name: app.globalData.userInfo.nickName,
      picture: app.globalData.userInfo.avatarUrl
    })
    var atkey = wx.getStorageSync("AccessToken");
    console.log(atkey);

    // --------- 发送凭证 ------------------
    wx.request({
      url: app.globalData.getHostpitalList,
      data: { AccessToken: atkey },
      header: {
        'content-type': 'application/json'
      },

      success: function (res) {
        that.setData({
          hospitalArray: res.data.datum.values,
          hospitalIDArray: res.data.datum.ids
        });
      },
      fail:function(){
        util.showRejectToast('加载医院信息失败！');
      }

    })

    // --------- 发送凭证 ------------------
    wx.request({
      url: app.globalData.getLevelList,
      data: { AccessToken: atkey, BIANMA:'002'},
      header: {
        'content-type': 'application/json'
      },

      success: function (res) {

        console.log(res.data);
        that.setData({
          levelArray: res.data.datum.values,
          levelIDArray: res.data.datum.ids
        });
      },
      fail: function () {
        util.showRejectToast('加载医生职称信息失败！');
      }

    })

  },

  onShow: function (options){
    app.userlogin();
  }



})
